package sm.routine;

import java.util.Timer;

import com.vmware.vim25.DynamicProperty;
import com.vmware.vim25.ManagedObjectReference;
import com.vmware.vim25.ObjectContent;
import com.vmware.vim25.ObjectSpec;
import com.vmware.vim25.PropertyFilterSpec;
import com.vmware.vim25.PropertySpec;
import com.vmware.vim25.RetrieveOptions;
import com.vmware.vim25.RetrieveResult;
import com.vmware.vim25.ServiceContent;
import com.vmware.vim25.TraversalSpec;
import com.vmware.vim25.VimPortType;
import com.vmware.vim25.VimServiceLocator;

import sm.namingservice.ServerState;
import sm.namingservice.ServerTable;
import sm.namingservice.ServerTuple;
import sm.routine.workload.WorkLoad;
import sm.server.serverlist.ServerList;
import sm.server.vmOperations.DwsDataRetriever;
import sm.utils.PropertySet;

public class WorkloadMonitor extends Thread{
	 private ManagedObjectReference _svcRef;
	 private VimServiceLocator _locator;
	 private VimPortType _service;
	 private ServiceContent _sic;
	
	private ServerTable _st;
	private ServerState _ss;
	private WorkLoad _wl = new WorkLoad();
	private final Long _delay = new Long(5000);
	private final Long _period = new Long(10000);
	
	public WorkloadMonitor(ServerTable _st, ServerState ss,ManagedObjectReference _svcRef,VimServiceLocator _locator,
			VimPortType _service,ServiceContent _sic){
		this._st = _st;
		this._ss = ss;
		this._svcRef = _svcRef;
		this._locator = _locator;
		this._service = _service;
		this._sic = _sic;
	}
	
	public void run(){
		try {
			initServer();
			
	//		Timer timer = new Timer(true);
	//		timer.schedule(new CheckGwsPowerState(_st,_ss,_wl,_svcRef,_locator,_service,_sic), _delay,_period);
	//		timer.schedule(new CheckGwsWorkLoad(_st,_ss,_wl,_svcRef,_locator,_service,_sic), _delay, _period);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void initServer() throws Exception{
		String[] dwsList = ServerList.getBList();
		String[] cList = ServerList.getCList();
		
		ServerList.setIp("51462231_dws_1", "10.12.0.108");
		ServerList.setIp("51462231_dws_2", "10.12.0.109");
		ServerList.setIp("51462231_dws_3", "10.12.0.110");
		ServerList.setIp("51462231_dws_c1", "10.12.0.137");
		ServerList.setIp("51462231_dws_c2", "10.12.0.114");
		ServerList.setIp("51462231_adminser", "10.12.0.111");
		
		RetrieveResult vmprops= DwsDataRetriever.retrieveData(
		this._sic, this._service,
		new String[]{"VirtualMachine"},"Virtual Lab/vm/cs6282/studentVM",
		new String[]{"name"});
		
		
		for(ObjectContent oc:vmprops.getObjects()){
			for(DynamicProperty dp:oc.getPropSet()){
		//		System.out.println(dp.getName()+"="+dp.getVal());
				if(dp.getName().equals("name")){
					String name = (String)dp.getVal();
					for(String s:dwsList){
						if(s.equals(name)){
							_ss.setDwsState(name, getPs(name));
							_st.addDws(name, new ServerTuple(name,ServerList.getIp(name),String.valueOf(PropertySet._dwsServiceDefaultSocket)));
							_wl.newMember(name);
						}
					}
					for(String s:cList){
						if(s.equals(name)){
							_ss.setDwsState(name, getPs(name));
							_st.addDws(name, new ServerTuple(name,ServerList.getIp(name),String.valueOf(PropertySet._dwsServiceDefaultSocket)));
							_wl.newMember(name);
						}
					}
				}
			}
		}
	}
	
	private String getIp(String name){
		String result = null;
		try {
			RetrieveResult ipprops= DwsDataRetriever.retrieveData(
					this._sic, this._service,
					new String[]{"VirtualMachine"},"Virtual Lab/vm/cs6282/studentVM",
					new String[]{"name","guest.ipAddress"});
			for(ObjectContent oc:ipprops.getObjects()){
				String curIp = null;
				for(DynamicProperty dp:oc.getPropSet()){
					if(dp.getName().equals("name")){
						if(dp.getVal().equals(name)){
							result = curIp;
							break;
						}
					}
					if(dp.getName().equals("guest.ipAddress")){
						curIp = dp.getVal().toString();
					}
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		
		return result;
	}
	private String getPs(String name){
		String result = null;
		try {
			RetrieveResult psprops= DwsDataRetriever.retrieveData(
					this._sic, this._service,
					new String[]{"VirtualMachine"},"Virtual Lab/vm/cs6282/studentVM",
					new String[]{"name","runtime.powerState"});
			for(ObjectContent oc:psprops.getObjects()){
				boolean next = false;
				for(DynamicProperty dp:oc.getPropSet()){
					if(dp.getName().equals("name")){
						if(dp.getVal().equals(name)){
							next = true;
						}
					}
					if(dp.getName().equals("runtime.powerState")){
						if(next){
							result = dp.getVal().toString();
							break;
						}
					}
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		
		return result;
	}
}
